<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Expansion\Room;
use Illuminate\Http\Request;

class ChatService
{
    use Service;

    /**
     * 聊天室首页
     *
     * @return Room[]|\Illuminate\Database\Eloquent\Collection
     */
    public function CharIndexService()
    {
        return Room::AllModel([ 'id', 'title' ]);
    }

    /**
     * 创建聊天室房间
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function CharRoomService( $request )
    {
        $validator = $this->getAdminValidator()->ChatRoomValidator($request);
        if ( $validator->first() ) return back()->with('error', $validator->first());
        $data[ 'title' ] = $request->input('title');
        Room::CreateModel($data);
        errorStorage('room', '"房间名" : ' . $request->input('title'), '创建房间', 'success', '创建房间');
        return back()->with('success', $this->getErrorCode()::message(207));
    }

    /**
     * 删除房间号
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function CharDestroyService( $request )
    {
        $validator = $this->getAdminValidator()->ChatRoomDeleteValidator($request);
        if ( $validator->first() ) return data(NULL, $validator->first(), 1000);
        $where = function ( $query ) use ( $request ) {
            $query->where('title', $request->input('title'));
        };
        Room::DeleteModel($where);
        errorStorage('room', '"房间名" : ' . $request->input('title'), '删除房间', 'success', '删除房间');
        return data();
    }
}
